﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.User;
public partial class car_insurance_AgentLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        General.ClearAllSession(this.Page, "ALL");
        if (Session[General.UserId] != null)
        {
            General.ClearAllSession(this.Page, "LOGOUT");
        }

        if (Request.Params["uid"] != null &&
            //Request.Params["uname"] != null && 
            Request.Params["cid"] != null &&
            //Request.Params["cemail"] != null &&
            //Request.Params["cphone"] != null &&
            // Request.Params["cname"] != null &&
            Request.Params["pcid"] != null)
        {
            Int64 _agentId = Convert.ToInt64(Request.Params["uid"].ToString());
            Int64 _customerId = Convert.ToInt64(Request.Params["cid"].ToString());
            if (CheckUserLoginCached(_customerId.ToString()))
            {
                Account _agentAccount = new Account();
                Account _customerAccount = new Account();
                AccountDB _myAccountDB = new AccountDB();

                Profile _agentProfile = new Profile();
                Profile _customerProfile = new Profile();
                ProfileDB _myProfileDB = new ProfileDB();

                Contact _customerContact = new Contact();
                ContactDB _myContactDB = new ContactDB();

                _agentAccount = _myAccountDB.GetAccount(_agentId);
                _customerAccount = _myAccountDB.GetAccount(_customerId);

                if (_agentAccount != null && _customerAccount != null)
                {
                    _agentProfile = _myProfileDB.GetProfile(_agentId);
                    _customerProfile = _myProfileDB.GetProfile(_customerId);

                    _customerContact = _myContactDB.GetContact(_customerId);
                }

                string _agentName = "";
                string _customerName = "";
                string _customerEmail = "";
                string _customerMobileNo = "";

                if (_agentProfile != null)
                {
                    _agentName = _agentProfile.SurName;
                    if (_agentProfile.FirstName.Length > 0)
                    {
                        _agentName += " " + _agentProfile.FirstName;
                    }
                }

                if (_customerProfile != null)
                {
                    _customerName = _customerProfile.SurName;
                    if (_customerProfile.FirstName.Length > 0)
                    {
                        _customerName += " " + _customerProfile.FirstName;
                    }
                }

                if (_customerContact != null)
                {
                    _customerEmail = _customerContact.Email;
                    _customerMobileNo = _customerContact.Mobile;
                }

                Session[General.AgentId] = _agentId.ToString();
                Session[General.AgentName] = _agentName;
                Session[General.UserId] = _customerId.ToString();
                Session[General.UserName] = _customerName;
                Session[General.UserEmail] = _customerEmail;
                Session[General.UserPhoneNo] = _customerMobileNo;
                Session[General.UserTypeId] = "400";
                Session[General.ProviderCompanyId] = Request.Params["pcid"].ToString();
                Session[General.UserProfileAndContactComplete] = "1";
                string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("IsCXLive");
                if (_strCXLive == "0")
                {
                    Session[CE.Global.General.Guest] = "User";
                }
                //if (Request.Params["carid"] != null &&
                //Request.Params["policyid"] != null)
                //{
                //    Session[General.CarId] = Request.Params["carid"].ToString();
                //    Session[General.CarPolicyId] = Request.Params["policyid"].ToString();
                //    Response.Redirect("CarAndPolicy_Details.aspx");
                //}
                //else
                //{
                //    Response.Redirect("Profile.aspx");
                //}
                Response.Redirect("personal-accident.aspx");
            }
            else
            {
                //Response.Redirect("UserMessage.aspx?ErrorCode=1993");
                Response.Redirect("~/user/usermessage.aspx?mt=UM&ec=1993");
            }
        }
        else if (Request.Params["uid"] != null && Request.Params["pcid"] != null)
        {
            Int64 _agentId = Convert.ToInt64(Request.Params["uid"].ToString());
            Account _agentAccount = new Account();
            Profile _agentProfile = new Profile();
            ProfileDB _myProfileDB = new ProfileDB();
            AccountDB _myAccountDB = new AccountDB();
            _agentAccount = _myAccountDB.GetAccount(_agentId);

            if (_agentAccount != null)
            {
                _agentProfile = _myProfileDB.GetProfile(_agentId);

                string _agentName = "";
                if (_agentProfile != null)
                {
                    _agentName = _agentProfile.SurName;
                    if (_agentProfile.FirstName.Length > 0)
                    {
                        _agentName += " " + _agentProfile.FirstName;
                    }
                }

                Session[General.AgentId] = _agentId.ToString();
                Session[General.AgentName] = _agentName;
                Session[General.ProviderCompanyId] = Request.Params["pcid"].ToString();
                string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("IsCXLive");
                if (_strCXLive == "0")
                {
                    Session[CE.Global.General.Guest] = "User";
                }
                Response.Redirect("personal-accident.aspx");
            }
        }
        else
        {
            lblMessage.Text = "Please close this page.";
        }

    }

    private bool CheckUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = CE.Configuration.MyConfigurationManager.GetAppSettingValue("UserCacheName");
        sKey += UserId;
        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            _retValue = false;
        }
        return _retValue;
    }
}
